package com.mxpio.erp.purchase.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.purchase.entity.BuyRequestOrderLine;
import com.mxpio.erp.common.purchase.service.BuyRequestOrderLineService;

import com.mxpio.erp.common.purchase.vo.RequestLineRejectVO;
import com.mxpio.erp.common.purchase.vo.RequestToOrderVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "BuyRequestOrderLineController", description ="采购申请单行数据接口" )
@RestController
@RequestMapping("/erp/purc/brline/")
public class BuyRequestOrderLineController {

    @Autowired
    private BuyRequestOrderLineService buyRequestOrderLineService;

    @GetMapping("list")
    @Operation(summary = "采购申请单行列表", description = "获取采购申请单行列表（无分页）", method = "GET")
    public Result<List<BuyRequestOrderLine>> list(@Parameter(name="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<BuyRequestOrderLine> buyRequestOrderLines = buyRequestOrderLineService.list(BuyRequestOrderLine.class, criteria);
        return Result.OK(buyRequestOrderLines);
    }

    @GetMapping("page")
    @Operation(summary = "采购申请单行列表", description = "获取采购申请单行列表（分页）", method = "GET")
    public Result<Page<BuyRequestOrderLine>> page(@Parameter(name="查询参数") Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BuyRequestOrderLine> buyRequestOrderLines = buyRequestOrderLineService.listPage(BuyRequestOrderLine.class,page, criteria);
        return Result.OK(buyRequestOrderLines);
    }

    @GetMapping("auditedPage")
    @Operation(summary = "采购申请单行列表(已审核)", description = "获取采购申请单行列表（分页）", method = "GET")
    public Result<Page<BuyRequestOrderLine>> auditedPage(@Parameter(name="查询参数") Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BuyRequestOrderLine> buyRequestOrderLines = buyRequestOrderLineService.listAuditedPage(page, criteria);
        return Result.OK(buyRequestOrderLines);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取采购申请单行", description = "根据bizNo获取采购申请单行", method = "GET")
    public Result<List<BuyRequestOrderLine>> getByBizNo(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<BuyRequestOrderLine> buyRequestOrderLines = buyRequestOrderLineService.list(BuyRequestOrderLine.class, c);
        return Result.OK(buyRequestOrderLines);
    }


    @GetMapping("list/{bizNo}/{lineNo}")
    @Operation(summary = "根据ID获取采购申请单行", description = "根据ID获取采购申请单行", method = "GET")
    public Result<BuyRequestOrderLine> getById(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo, @Parameter(name="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        OrderLineKey lineKey = new OrderLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setBizNo(bizNo);
        BuyRequestOrderLine buyRequestOrderLine = buyRequestOrderLineService.getById(BuyRequestOrderLine.class, lineKey);
        return Result.OK(buyRequestOrderLine);
    }

    @PostMapping("add")
    @Operation(summary = "新增采购申请单行", description = "新增采购申请单行", method = "POST")
    public Result<BuyRequestOrderLine> add(@RequestBody BuyRequestOrderLine buyRequestOrderLine) {
        buyRequestOrderLine.setLineStatus(OrderStatusEnums.BuyRequestLineStatus.CREATE.getStatusCode());
        buyRequestOrderLineService.save(buyRequestOrderLine);
        return Result.OK(buyRequestOrderLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑采购申请单行", description = "编辑采购申请单行", method = "PUT")
    public Result<BuyRequestOrderLine> edit(@RequestBody BuyRequestOrderLine buyRequestOrderLine) {
        buyRequestOrderLineService.update(buyRequestOrderLine);
        return Result.OK(buyRequestOrderLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除采购申请单行", description = "删除采购申请单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "DELETE")
    public Result<BuyRequestOrderLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            buyRequestOrderLineService.remove(BuyRequestOrderLine.class, lineKey);
        }
        return Result.OK();
    }

    @PutMapping("reject")
    @Operation(summary = "驳回采购申请单行", description = "驳回采购申请单行", method = "PUT")
    public Result<BuyRequestOrderLine> reject(@RequestBody RequestLineRejectVO vo) {
        String[] id = vo.getIds().split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            BuyRequestOrderLine line = buyRequestOrderLineService.getById(BuyRequestOrderLine.class,lineKey);
            line.setCloseStatus(OrderStatusEnums.CloseStatus.CLOSED.getStatusCode());
            line.setLineStatus(OrderStatusEnums.BuyRequestLineStatus.CLOSE.getStatusCode());
            line.setReason(vo.getReason());
            buyRequestOrderLineService.update(line);
        }
        return Result.OK();
    }

    @PutMapping("exec")
    @Operation(summary = "转为采购订单", description = "转为采购订单", method = "PUT")
    public Result<BuyRequestOrderLine> exec(@RequestBody RequestToOrderVO vo) {
        buyRequestOrderLineService.execToPurcOrder(vo);
        return Result.OK();
    }
}

